<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>nginx日志切割 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>nginx日志切割</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LNMP/">LNMP</a> <a class="article__tag-link" href="/tags/Nginx/">Nginx</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nginx 没有 apache自动切割的工具，只有写脚本，也可以借助 centos 自带的日志归档工具 logrotate 。</p>
<h2 id="1-nginx-日志切割脚本"><a href="#1-nginx-日志切割脚本" class="headerlink" title="1.nginx 日志切割脚本"></a>1.nginx 日志切割脚本</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先确定访问日志路径，假定为 /tmp/axxess.log ，还要确定 nginx 的 pid 文件所在路径，假定为 /usr/local/nginx/var/nginx.pid。下面开始写日志</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># vim /usr/local/sbin/nginx_logrotate.sh</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">d=`data -d <span class="string">"-1 day"</span> +%F`</div><div class="line">[ -d /tmp/nginx_log ] || mkdir /tmp/nginx_log</div><div class="line">mv /tmp/access.log /tmp/nginx_log/<span class="variable">$d</span>.<span class="built_in">log</span></div><div class="line">/etc/init.d/nginx reload &gt; /dev/null</div><div class="line"><span class="built_in">cd</span> /tmp/nginx_log/</div><div class="line">gizp -f <span class="variable">$d</span>.<span class="built_in">log</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;d=<code>data -d &quot;-1 day&quot; +%F</code> 表示时间，切割前一天的日志；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;[ -d /tmp/nginx_log ] || mkdir /tmp/nginx_log 判断归档目录存在不存在，存在就进行下一步，不存在就创建； </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;mv /tmp/access.log /tmp/nginx_log/$d.log 把日志移动到归档目录，文件名$d根据时创建；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/etc/init.d/nginx reload &gt; /dev/null 移动日志以后需要重新创建access.log 重新加载 nginx 就可以，然后把重新加载的输出日志重定向到 /dec/null；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cd /tmp/nginx_log/ 进入归档目录；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cd /tmp/nginx_log/ 如果日志比较大，压缩归档文件方便保存，-f 表示强制覆盖。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后写一个计划任务，每天0点0分执行脚本。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># crontab -e</span></div><div class="line"></div><div class="line">0 0 * * * /bin/bash /usr/<span class="built_in">local</span>/sbin/nginx_logrotate.sh</div></pre></td></tr></table></figure>
<h2 id="2-借助系统的-logrotate-工具实现"><a href="#2-借助系统的-logrotate-工具实现" class="headerlink" title="2.借助系统的 logrotate 工具实现"></a>2.借助系统的 logrotate 工具实现</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># vim /etc/logrotate.d/nginx</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">/tmp/*.<span class="built_in">log</span> &#123;</div><div class="line">Daily</div><div class="line">Missingok</div><div class="line">rotate 52</div><div class="line">compress</div><div class="line">delaycompress</div><div class="line">notifempty</div><div class="line">create 644 nobody nobody</div><div class="line">sharedscripts</div><div class="line">postrotate</div><div class="line">[ -f /usr/<span class="built_in">local</span>/nginx/var/nginx.pid ] &amp;&amp; <span class="built_in">kill</span> -USR1 `cat /usr/<span class="built_in">local</span>/nginx/var/nginx.pid`</div><div class="line">Endscript</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：</p>
<ul>
<li>daily 表示日志按天归档；</li>
<li>missingok 表示忽略所有错误，比如日志文件不存在的情况下；</li>
<li>rotate 52 表示存放的日志个数，最多就52个，最老的会被删除；</li>
<li>compress 表示日志要压缩；</li>
<li>delaycopress 表示压缩除了当前和最近之外的所有其他版本；</li>
<li>notifempty 表示如果日志为空，则不归档；</li>
<li>create 644 nobody nobody 定义归档日志的权限以及属主和属组；</li>
<li>sharedscripts 表示所有的日志共享该脚本，因为这里指定的日志文件为多个，用来*.log；</li>
<li>portrotate 后边跟轮换过日志之后要运行的命令；</li>
<li>endscript 表示结束了。</li>
</ul>

	

	
		<span class="different-posts"><a href="/2017/10/12/Nginx/18. nginx日志切割/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
